<?php

namespace App\Service\WorkOrder\Message;

use App\Enums\WorkOrder\ErrCode;
use App\Enums\WorkOrder\OrderEnum;
use App\Exceptions\Custom\CustomHttpResponseException;
use App\Http\Middleware\OpenShop\UserPermissionAuth;
use App\Library\Rms\RmsUserApi;
use App\Models\WorkOrder\WorkOrderApplyMainModel;
use App\Service\Common\SmsService;
use App\Service\OpenShop\Feishu\MessageSendService;
use App\Service\OpenShop\User\RmsUserService;
use Illuminate\Support\Facades\Log;

class BaseMessageService
{
    protected  ?WorkOrderApplyMainModel $workOrderApplyMainModel;

    // 加盟商信息
    protected string $phone;
    protected string $name;

    public function __construct(string $workOrderNo)
    {
        $this->workOrderApplyMainModel = WorkOrderApplyMainModel::query()->where('workOrderNo', $workOrderNo)->first();
        if (!$this->workOrderApplyMainModel) {
            throw new CustomHttpResponseException(ErrCode::errCodeTrans(ErrCode::CODE_ERR_WORK_ORDER_NO));
        }

        $this->phone = $this->workOrderApplyMainModel->extraArr['mobile'] ?? '';

        if ($this->phone) {
            $user = (new RmsUserApi())->getUserInfoByMobiles([$this->phone]);
            $this->name = $user['data'][0]['username'] ?? '';
        } else {
            $user = RmsUserService::getRmsUserInfoByUserIds([$this->workOrderApplyMainModel->createdByUid]);
            $this->name = $user['data'][0]['username'] ?? '';
            $this->phone = $user['data'][0]['mobile'] ?? '';
        }

        if (!$this->name) {
            $this->name = '用户';
        }
    }

//    public function __get()
//    {
//
//    }
}
